package com.xiaoluyy.demo.config;

import com.xiaoluyy.demo.api.DemoService1Impl;
import com.xiaoluyy.demo.common.util.ThriftRpcService;
import com.xiaoluyy.demo.common.util.ThriftRpcServiceConfig;
import com.xiaoluyy.demo.thrift.DemoService1;
import org.apache.commons.lang.StringUtils;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.transport.TTransportFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * thrift service configuration
 */
@Configuration
public class ThriftServiceConfig {

    @Value("${thrift.service.demo.name}")
    String thriftServiceName;

    //@Value("${thrift.service.demo.configfile}")
    private String thriftServiceConfFile;

    @Value("${thrift.service.demo.transportFactoryName}")
    private String transportFactoryName;;

    @Value("${thrift.service.demo.protocolFactoryName}")
    private String protocolFactoryName;;

    @Value("${thrift.service.demo.serverType}")
    private String serverType;

    @Value("${thrift.service.demo.registerFlag}")
    private String registerFlag;

    @Autowired
    private DemoService1Impl demoService1;

    @Bean(name = "thriftRpcService",initMethod = "startup", destroyMethod = "shutdown")
    public ThriftRpcService getThriftRpcService() {

        ThriftRpcService thriftRpcService = adaptThriftRpcService();
        thriftRpcService.setThriftServiceName(thriftServiceName);

        List<ThriftRpcServiceConfig> serviceConfigs = new ArrayList<ThriftRpcServiceConfig>();

        //有其它thrift service时，继续添加
        // demoService1
        ThriftRpcServiceConfig demoService1Config = new ThriftRpcServiceConfig();
        demoService1Config.setServiceClazz(DemoService1.class.getName());
        demoService1Config.setServiceImpl(demoService1);
        demoService1Config.setRegisteFlag(Boolean.valueOf(registerFlag));
        serviceConfigs.add(demoService1Config);
        // demoService2
        /*ThriftRpcServiceConfig demoService2Config = new ThriftRpcServiceConfig();
        demoService2Config.setServiceClazz(DemoService2.class.getName());
        demoService2Config.setServiceImpl(demoService2);
        demoService2Config.setRegisteFlag(Boolean.valueOf(registerFlag));
        serviceConfigs.add(demoService2Config);*/

        thriftRpcService.setServiceConfigList(serviceConfigs);
        return thriftRpcService;
    }

	private ThriftRpcService adaptThriftRpcService() {
		TTransportFactory transportFactory = null;
		TProtocolFactory protocolFactory = null;

		if(StringUtils.isBlank(transportFactoryName) || transportFactoryName.equals("TTransportFactory"))
		{
			transportFactory = new TTransportFactory();
		}

		if(StringUtils.isBlank(protocolFactoryName) || protocolFactoryName.equals("TBinaryProtocol"))
		{
			protocolFactory = new TBinaryProtocol.Factory();
		}

		ThriftRpcService thriftRpcService = new ThriftRpcService(transportFactory,protocolFactory,serverType);
		return thriftRpcService;
	}

}
